package com.dao;

import com.entity.TArticle;
import com.entity.TArticleExample;
import com.mapper.TArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by del on 16-5-1.
 */
@Component
public class TArticleDao {

    @Autowired
    private TArticleMapper tArticleMapper;

    public boolean insert(TArticle tArticle){
        return tArticleMapper.insert(tArticle) == 1;
    }

    public boolean delete(Integer id){
        return tArticleMapper.deleteByPrimaryKey(id) == 1;
    }

    public TArticle update(TArticle tArticle){
        return tArticleMapper.updateByPrimaryKey(tArticle) == 1 ? tArticle : null;
    }

    public TArticle select(Integer id){
        return tArticleMapper.selectByPrimaryKey(id);
    }

    public List<TArticle> selectExample(TArticleExample tArticleExample) {
        return tArticleMapper.selectByExample(tArticleExample);
    }

    public TArticle selectByAid(String aid) {
        TArticleExample tArticleExample = new TArticleExample();
        tArticleExample
                .createCriteria()
                .andAidEqualTo(aid);
        List<TArticle> tArticleList = tArticleMapper.selectByExample(tArticleExample);
        if (!tArticleList.isEmpty()){
            return tArticleList.get(0);
        }
        else {
            return null;
        }
    }

    public Integer selectCount(TArticleExample tArticleExample) {
        return tArticleMapper.countByExample(tArticleExample);
    }

    public List<TArticle> selectByPage(Integer page, Integer pageSize) {
        return tArticleMapper.selectByPage(page,pageSize);
    }

    public TArticle selectNewest() {
        return tArticleMapper.selectNewest();
    }
}
